import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:oral_cloud/config/constant.dart';
import 'package:oral_cloud/generated/l10n.dart';
import 'package:oral_cloud/ui/widget/global_widget.dart';
import 'package:oral_cloud/ui/widget/image.dart';
import 'package:oral_cloud/ui/widget/seekbar/seekbar.dart';
import 'package:photo_view/photo_view_gallery.dart';

class MedicalImagePreview extends StatefulWidget {
  List images = [];
  int index = 0;
  String heroTag;
  PageController controller;
  String placeholder;

  MedicalImagePreview(
      {Key key,
      @required this.images,
      this.index,
      this.controller,
      this.heroTag,
      this.placeholder: 'certificate_default.png'})
      : super(key: key) {
    controller = PageController(initialPage: index);
  }

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MedicalImagePreviewState();
  }
}

class _MedicalImagePreviewState extends State<MedicalImagePreview> {
  GlobalKey<SeekTextState> seekTextKey = GlobalKey();
  GlobalKey<_MySeekBarState> seekBarKey;
  int currentIndex = 0;
  final double bottomBarHeight=ScreenUtil.getInstance().setHeight(158);

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    currentIndex = widget.index;
  }



  @override
  Widget build(BuildContext context) {
    AppBar appBar=AppBar(
      centerTitle: true,
      brightness: Brightness.dark,
      backgroundColor: Color(0xff3a3a3a),
      iconTheme: IconThemeData(
        color: Colors.white, //change your color here
      ),
      title: Text(
        S.of(context).imgPreview,
        style: TextStyle(color: Colors.white, fontSize: FontSize.fontSizeXL),
      ),
    );
    double photoHeight=MediaQuery.of(context).size.height-appBar.preferredSize.height-bottomBarHeight-MediaQuery.of(context).padding.top;
    double photoWidth=MediaQuery.of(context).size.width;
    print('photoHeight=$photoHeight,photoWidth=$photoWidth');
    List<Widget> bottomChildren=[];
    if(widget.images.length>10){
      seekBarKey= GlobalKey();
      bottomChildren=[
        SizedBox(height: 8,),
        Container(
          width: ScreenUtil.getInstance().setWidth(702),
          child: MySeekBar(seekBarKey, widget.images.length, widget.controller),
        ),
        SizedBox(height: 8,),
        SeekText(seekTextKey,widget.images.length,currentIndex: 0,),
      ];
    }else{
      bottomChildren=[SeekText(seekTextKey, widget.images.length),];
    }
    return Scaffold(
      appBar: appBar,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Expanded(
            child: Container(
                child: PhotoViewGallery.builder(
              scrollPhysics: const BouncingScrollPhysics(),
              builder: (BuildContext context, int index) {
                return PhotoViewGalleryPageOptions.customChild(
                  child: WrapperImage(
                    url: widget.images[index],
                    placeholder: widget.placeholder,
                  ),
                  childSize: Size(photoWidth,
                      photoHeight),
                  heroAttributes: null,
                );
              },
              itemCount: widget.images.length,
              loadingChild: Container(),
              backgroundDecoration: null,
              pageController: widget.controller,
              enableRotation: true,
              onPageChanged: (index) {
                print('onPageChanged---->index=$index');
                seekBarKey?.currentState?.updateCurrentIndex(index);
                seekTextKey.currentState.updateCurrentIndex(index);
              },
            )),
          ),
          Stack(
            children: [
              Container(
                color: Color(0xff3a3a3a),
                width: double.infinity,
                height: bottomBarHeight,
                child: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: bottomChildren,
                  ),
                ),
              ),
              Positioned(//图片index显示
                top: 1,
                width: MediaQuery.of(context).size.width,
                child: HorizontalLine(),
              ),
            ],
          ),

        ],
      ),
    );
  }
}

class MySeekBar extends StatefulWidget{
  final int totalLength;
  final PageController controller;
  MySeekBar(Key key,this.totalLength, this.controller): super(key: key);
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MySeekBarState();
  }

}

class _MySeekBarState extends State<MySeekBar>{
  int currentIndex=0;
  bool fromUser=false;
  void updateCurrentIndex(int index){
    if(fromUser){
      fromUser=false;
      return;
    }
    setState(() {
      currentIndex = index;
      print('_MySeekBarState----->updateCurrentIndex---->currentIndex=$currentIndex');
    });
  }

  @override
  Widget build(BuildContext context) {
    return SeekBar(
      progressColor: Theme.of(context).primaryColor,
      backgroundColor: Theme.of(context).primaryColor,
      progressHeight: ScreenUtil.getInstance().setHeight(20),
      max: (widget.totalLength-1).toDouble(),
      value: currentIndex.toDouble(),
      sectionCount: 1,
      onValueChanged: (value){
        fromUser=true;
        print('onValueChanged--->value=$value');
        int index=(value.progress*(widget.totalLength-1)).toInt();
        print('onValueChanged--->currentIndex=$currentIndex');
        widget.controller.jumpToPage(index);
      },
    );
  }

}



class SeekText extends StatefulWidget{
  int totalLength;
  int currentIndex;
  final Color color;
  SeekText(Key key,this.totalLength, {this.color:Colors.white,this.currentIndex:0}): super(key: key);
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return SeekTextState();
  }

}

class SeekTextState extends State<SeekText>{
  void updateCurrentIndex(int index,{int totalLength}){
    setState(() {
      widget.currentIndex = index;
      if(totalLength!=null) {
        widget.totalLength = totalLength;
      }
      print('_SeekTextState---->updateCurrentIndex---->currentIndex=$widget.currentIndex,totalLength=$totalLength');
    });
  }

  @override
  Widget build(BuildContext context) {
    widget.currentIndex=widget.currentIndex>=widget.totalLength?(widget.totalLength-1):widget.currentIndex;
    return Text("${(widget.currentIndex+1)}/${widget.totalLength}",
        style: TextStyle(color: widget.color, fontSize: FontSize.fontSizeXL));
  }

}
